*********************************************************
* Analysis Part 3: Table 9 Columns 2-4
* 
* Topic: The Value of Ratings: Evidence from their Introduction in Securities Markets
* Authors: Asaf Bernstein, Carola Frydman, Eric Hilt
* 
* Code last updated: 5/23/25
* 
*************************************************************

*************************************************************
* I. Initial Code Settings/Directories
*************************************************************

clear all
* Set to folder of where do file is saved. Change if necessary.
cd "`=substr(c(current_do), 1, indexnot(c(current_do), "/", -1))'"

log using "Part4_BlockSize_Log.txt", text replace

*************************************************************
* II. Load in data & define variable
*************************************************************
	
use "IntradayData.dta", clear

* Merge in instrument
	
    drop if date_bidask==-18432
	merge m:1 id_issue_bidask using "spreads_instrument_bidask_data_cross-section.dta"
	keep if _merge==3
	drop _merge

* Create dvs for columns 2-4

    gen num_bonds_sold_eq1 = sales_tras==1
    bys id_issue_bidask date_bidask: egen num_trans_eq1 = sum(num_bonds_sold_eq1)
    label variable num_trans_eq1 "# one-lot trades" 	

    gen prob_trans_eq1 = num_trans_eq1>0
    label variable prob_trans_eq1 "prob(one-lot trade)"

    xtile ptiles_sales_tras = sales_tras  if othry!=. , n(100)
    gen num_bonds_sold_p75 = ptiles_sales_tras>=75
    bys id_issue_bidask date_bidask: egen num_trans_p75 = sum(num_bonds_sold_p75)
    label variable num_trans_p75 "# large trades (75th pctile)"  

* Create main interactions for regressions
	
    sort date_bidask
    egen date_std = group(date_bidask)

    gen Post = date_std>=14

    gen post_rated = Post*rated_issue_manual
    gen trend_rated = date_std*rated_issue_manual

    gen post_othry = Post*othry
    gen trend_othry = date_std*othry

    gen weeksSinceTreat = date_std - 13

    drop trade_num_tras
    drop num_bonds_sold_*

    drop price_tras sales_tras ptiles_sales_tras

    duplicates drop	

    egen id_issue_bidask_gp = group(id_issue_bidask)
	
*************************************************************
* III. Analysis/output
*************************************************************

global file_name_new_v1 "T9"
global tbl_title "Table 9. Effect of Ratings on Block Size"


*************************************************************
* Table 9 (columns 2-4 only)
*************************************************************

* Column 2

    ivreghdfe num_trans_eq1 (post_rated trend_rated = post_othry trend_othry), absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)	
    est store t9c2
    
    *F stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Mean of dv; number of bonds (rated and unrated)
    preserve		
	   keep if e(sample)==1
	   quietly sum num_trans_eq1
	   local mean_y = r(mean)
	   display "Mean dep var: "  `mean_y'
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Weak instrument robust confidence sets
    twostepweakiv 2sls num_trans_eq1 (post_rated trend_rated = post_othry trend_othry) i.date_std i.id_issue_bidask_gp c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std, cluster(id_issue_bidask_gp)	project(post_rated)


* Column 3

    ivreghdfe prob_trans_eq1 (post_rated trend_rated = post_othry trend_othry), absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)	
    est store t9c3
    
    *F stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Mean of dv; number of bonds (rated and unrated)
    preserve		
	   keep if e(sample)==1
	   quietly sum prob_trans_eq1
	   local mean_y = r(mean)
	   display "Mean dep var: "  `mean_y'
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore
    *Weak instrument robust confidence sets
    twostepweakiv 2sls prob_trans_eq1 (post_rated trend_rated = post_othry trend_othry) i.date_std i.id_issue_bidask_gp c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std, cluster(id_issue_bidask_gp)	project(post_rated)


* Column 4

    ivreghdfe num_trans_p75 (post_rated trend_rated = post_othry trend_othry), absorb( date_std id_issue_bidask c.PreMean_YTM_Bid##i.date_std c.PreMean_Spread_pct##i.date_std) cluster(id_issue_bidask)	
    est store t9c4

    *F stat
    matrix first_stage = e(widstat)
	local first_f = first_stage[1,1]
	display "Kleibergen-Paap F, First Stage: " `first_f'
    *Mean of dv; number of bonds (rated and unrated)
    preserve		
	   keep if e(sample)==1
	   quietly sum num_trans_p75
	   local mean_y = r(mean)
	   display "Mean dep var: "  `mean_y'
	   duplicates drop id_issue_bidask, force
	   tab rated_issue_manual
    restore

* Create table of results

    estimates use t9c1.ster
    est store t9c1

    esttab t9c1 t9c2 t9c3 t9c4 using "$file_name_new_v1.rtf", replace compress nogaps se ///
	   title($tbl_title) ///
	   addnote("") ///
	   label interaction(" X ") starlevels("{\super *}" 0.10 "{\super **}" 0.05 "{\super ***}" 0.01)

    tempname handle4
    rtfappend `handle4' using "$file_name_new_v1.rtf", replace
    file write `handle4' "\page"
    rtfclose `handle4'

    erase t9c1.ster



log close	
	
	
	
	
	
	
